﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Web.Script.Serialization;

public class Yb
{
    public static dynamic getYbBackObj(string jybh, Dictionary<string, object> data_dict, bool flag = false)
    {
        return getYbBackObj(jybh, data_dict, string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, flag);
    }

    public static dynamic getYbBackObj(string jybh, Dictionary<string, object> data_dict, string opter, string opter_name, bool flag = false)
    {
        return getYbBackObj(jybh, data_dict, opter, opter_name, string.Empty, string.Empty, string.Empty, flag);
    }

    public static dynamic getYbBackObj(string jybh, Dictionary<string, object> data_dict, string opter, string opter_name, string mdtrtarea_admvs, string insuplc_admdvs, bool flag = false)
    {
        return getYbBackObj(jybh, data_dict, opter, opter_name, mdtrtarea_admvs, insuplc_admdvs, string.Empty, flag);
    }

    /// <summary>
    ///  获取医保返回对象
    /// </summary>
    /// <param name="jybh">交易编号</param>
    /// <param name="data_dict">数据体</param>
    /// <param name="opter">操作人工号</param>
    /// <param name="opter_name">操作人姓名</param>
    /// <param name="mdtrtarea_admvs">就医地</param>
    /// <param name="insuplc_admdvs">参保地</param>
    /// <param name="sign_no">签到号</param>
    /// <returns></returns>
    public static dynamic getYbBackObj(string jybh, Dictionary<string, object> data_dict, string opter, string opter_name, string mdtrtarea_admvs, string insuplc_admdvs, string sign_no, bool flag = false)
    {
        dynamic sss = new ExpandoObject();

        DataTable table = GetDataFromSqlLite.GetSourceCode();
        string t_trade = table.AsEnumerable().FirstOrDefault(r => r.Field<string>("jybh") == jybh)?["jysr"]?.ToString();
        string t_trade_out = table.AsEnumerable().FirstOrDefault(r => r.Field<string>("jybh") == jybh)?["jysc"]?.ToString();
        string json = table.AsEnumerable().FirstOrDefault(r => r.Field<string>("jybh") == "json")?["jysr"]?.ToString();
        string yb = table.AsEnumerable().FirstOrDefault(r => r.Field<string>("jybh") == "yb")?["jysr"]?.ToString();

        string input = DynamicLoader.getMethod(t_trade, new Type[] { typeof(Dictionary<string, object>) }, new object[] { data_dict }).ToString();

        var input_dict = new Dictionary<string, object> { { "infno", jybh }, { "input", input }, { "sign_no", sign_no }, { "opter", opter }, { "opter_name", opter_name }, { "mdtrtarea_admvs", mdtrtarea_admvs }, { "insuplc_admdvs", insuplc_admdvs }, };
        string input_json = DynamicLoader.getMethod(json, new Type[] { typeof(Dictionary<string, object>) }, new object[] { input_dict }).ToString();

        var jysr_dict = new Dictionary<string, object> { { "infno", jybh }, { "jysr", input_json }, };
        string output_json = DynamicLoader.getMethod(yb, new Type[] { typeof(Dictionary<string, object>) }, new object[] { jysr_dict }).ToString();

        sss.infno = jybh;
        sss.input = input;
        sss.input_json = input_json;
        sss.output_json = output_json;
        sss.infcode = -1;
        sss.err_msg = string.Empty;
        sss.output = string.Empty;
        if (flag)
        {
            Logger.Log("入参:" + input_json);
            Logger.Log("出参:" + output_json);
        }

        if (!"".Equals(output_json))
        {


            try
            {
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                serializer.MaxJsonLength = int.MaxValue;
                Dictionary<string, object> rootObject = serializer.Deserialize<Dictionary<string, object>>(output_json);

                sss.infcode = Convert.ToInt32(rootObject["infcode"]);
                sss.err_msg = Convert.ToString(rootObject["err_msg"]);
                var outputObject = rootObject["output"] as Dictionary<string, object>;
                //entity.output = Convert.ToString(serializer.Serialize(outputObject));

                sss.output = DynamicLoader.getOutput(t_trade_out, Convert.ToString(serializer.Serialize(outputObject)));
            }
            catch
            {

            }
        }
        //sss.output = DynamicLoader.getOutput(t_trade_out, output_json);
        return sss;
        // return DynamicLoader.getOutput(t_trade_out, output_json);
    }

    /// <summary>
    /// 获取入参样式
    /// </summary>
    /// <param name="jybh">交易编号</param>
    /// <returns></returns>
    public static string getYbBackObjTest(string jybh)
    {
        DataTable table = GetDataFromSqlLite.GetSourceCode();
        string t_trade = table.AsEnumerable().FirstOrDefault(r => r.Field<string>("jybh") == jybh)?["jysr"]?.ToString();
        return  DynamicLoader.getMethod(t_trade, null, null).ToString();
    }
}

